#!/usr/bin/perl

use strict;
use warnings;

use LWP;

# TODO: Need a loop around these

#my $site = 'http://asiaforum.no/viewforum.php?f=21';
#my $site = 'http://asiaforum.no/viewforum.php?f=21&topicdays=0&start=50';
#my $site = 'http://asiaforum.no/viewforum.php?f=21&topicdays=0&start=100';

#my $site = 'http://asiaforum.no/viewforum.php?f=29';
#my $site = 'http://asiaforum.no/viewforum.php?f=30';
#my $site = 'http://asiaforum.no/viewforum.php?f=31';
#my $site = 'http://asiaforum.no/viewforum.php?f=32';
#my $site = 'http://asiaforum.no/viewforum.php?f=60';

my $site = 'http://asiaforum.no/viewforum.php?f=2';

my $repository = 'repository';

# leser inn db
my %newsitems = ();
open(REP, "<$repository") || die "Can not open $repository";
while(<REP>) {
	next if /^#/;
	chop;
	my($url,$title) = split(/-->>/);
	next if $url =~ /^\s*$/;
	next if $title =~ /^\s*$/;
	$newsitems{$url} = $title;
}
close(REP);

my $browser = LWP::UserAgent->new;
my $response = $browser->get($site);
die "Can't get $site -- ", $response->status_line unless $response->is_success;
my $content = $response->content;

#print $content;
# Finne alle innlegg og behandle dem

while($content =~ /<a href=\"viewtopic.php\?t=(\d+)?.*\" class="topictitle">(.+)<\/a><\/span>/mg) {
	$newsitems{'http://asiaforum.no/viewtopic.php?t=' . $1} = $2;
} 


open(REP, ">$repository") || die "Can not open $repository";
foreach my $url (sort keys %newsitems) {
	next if $url =~ /^\s*$/;
	print REP $url . "-->>" . $newsitems{$url} . "\n";
}
close(REP);
